package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.OrderSetting;
import com.itheima.sercive.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void addOrderSettingList(List<OrderSetting> orderSettingList) {
        if(orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                saveOrEdit(orderSetting);
            }
        }
    }

    private void saveOrEdit(OrderSetting orderSetting) {
        //根据日期从数据库查询预约设置对象
        OrderSetting orderSettingDb  = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        //如果能查到预约设置对象，修改
        if(orderSettingDb != null){
            //如果已预约大于可预约， 不能设置, 抛出异常
//            if(orderSettingDb.getReservations() > orderSetting.getNumber()){
//                throw new BusinessRuntimeException("已预约大于可预约，不能设置!!");
//            }
            orderSettingDao.edit(orderSetting);
        }else{
            //如果查不到预约设置对象，添加
            orderSettingDao.add(orderSetting);
        }
    }
}
